package com.example.hospitalmanagementapp;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ShowableListMenu;
import androidx.fragment.app.FragmentTransaction;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class MainActivity extends AppCompatActivity {

    private List<BaseFragment> fragments;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initView() {
        BottomNavigationView bnv = findViewById(R.id.home_navi);
        fragments = new ArrayList<>();
        fragments.add(new ComplexFragment());
        fragments.add(new ServiceFragment());
        fragments.add(new PersonFragment());

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragments.forEach(fragment -> {
            fragmentTransaction.add(R.id.Diagnostic_services, fragment);
            if (fragment instanceof ComplexFragment) {
                fragmentTransaction.show(fragment);
            }
        });

        fragmentTransaction.commit();
        bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                showSelectFragment(item.getItemId());
                return true;
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showSelectFragment(int itemId) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragments.forEach(new Consumer<BaseFragment>() {
            @Override
            public void accept(BaseFragment baseFragment) {
                fragmentTransaction.hide(baseFragment);
            }
        });

        switch (itemId) {
            case R.id.hospital_complex1:
                fragmentTransaction.show(fragments.get(0));
                break;
            case R.id.Diagnostic_services:
                fragmentTransaction.show(fragments.get(1));
                break;
            case R.id.personal_center:
                fragmentTransaction.show(fragments.get(2));
                break;
            default:
        }
        fragmentTransaction.commit();
    }
}